#!/bin/bash

# 生成当前时间戳
TIMESTAMP=`date +%y-%m-%d_%H:%M:%S`

# 获取项目可执行 jar 文件
OBJ_DIR=`pwd | sed 's/\/opt\///g'`
SRC_FILE="${OBJ_DIR}-3.0.0-SNAPSHOT.jar"

# 项目 PID
OBJ_PID=`ps -eo pid,comm,cmd | grep ${SRC_FILE} | grep -v 'grep' | grep -v 'sh' | sed 's/\s//g' | sed 's/java.*//g'`
# 判断项目是否正在运行
if [ -z ${OBJ_PID} ]; then
    printf "\033[30;31mError：项目\"\033[30;32m${SRC_FILE}\033[30;31m\" PID不存在！\033[0m \n"
	echo
    exit
fi

# 备份旧项目包
LOGIN_IP=`who -u am i 2>/dev/null | awk '{ print $NF }' |sed -e 's/[()]//g'`
\cp ${SRC_FILE} ${SRC_FILE}.bak.${TIMESTAMP}_${LOGIN_IP}

# 杀死项目后重启
kill -15 ${OBJ_PID}
echo "杀死 ${SRC_FILE} -> ${OBJ_PID}"

